//
//  AMGetIntegralView.m
//  DemoForSignIn
//
//  Created by  高东星 on 17/8/28.
//  Copyright © 2017年 anmur. All rights reserved.
//

#import "AMGetIntegralView.h"

@interface AMGetIntegralView ()

@property (nonatomic,weak) UILabel *dayCountLabel;
@property (nonatomic,weak) UIButton *getIntegralButton;
@property (nonatomic,weak) UIActivityIndicatorView *activityIndicatorView;
@end


@implementation AMGetIntegralView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addDayCountLabel];
        [self addGetIntegralButton];
        [self addActivityIndicatorView];
        self.state = AMGetIntegralViewGetStateNormal;
        self.backgroundColor = [UIColor redColor];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat height = 20;
    _getIntegralButton.frame = CGRectMake(0, self.frame.size.height - height, self.frame.size.width, height);
    _getIntegralButton.layer.borderWidth = 1.0 / [UIScreen mainScreen].scale;
    _getIntegralButton.layer.borderColor = [UIColor darkGrayColor].CGColor;
    _getIntegralButton.layer.cornerRadius = 3;
    _dayCountLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - height);
    _activityIndicatorView.center = _getIntegralButton.center;
}

- (void)setState:(AMGetIntegralViewGetState)state{
    switch (state) {
        case AMGetIntegralViewGetStateCompleted:
            [_activityIndicatorView stopAnimating];
            _activityIndicatorView.hidden = YES;
            _getIntegralButton.hidden = NO;
            [_getIntegralButton setTitle:@"已领取" forState:UIControlStateNormal];
            _getIntegralButton.backgroundColor = [UIColor redColor];
            _getIntegralButton.userInteractionEnabled = NO;
            break;
        case AMGetIntegralViewGetStateNormal:
            [_activityIndicatorView stopAnimating];
            _activityIndicatorView.hidden = YES;
            _getIntegralButton.hidden = NO;
            [_getIntegralButton setTitle:@"领取" forState:UIControlStateNormal];
            _getIntegralButton.backgroundColor = [UIColor lightGrayColor];
            _getIntegralButton.userInteractionEnabled = YES;
            break;
        case AMGetIntegralViewGetStateGetting:
            _activityIndicatorView.hidden = NO;
            [_activityIndicatorView startAnimating];
            _getIntegralButton.hidden = YES;
            break;
        default:
            break;
    }
}


- (void)getIntegralButtonDidClick{
    if ([self.delegate respondsToSelector:@selector(getIntegralViewGetButtonDidClick:)]) {
        [self.delegate getIntegralViewGetButtonDidClick:self];
    }
}

- (void)addDayCountLabel{
    UILabel *dayCountLabel = [UILabel new];
    dayCountLabel.font = [UIFont systemFontOfSize:12];
    dayCountLabel.textColor = [UIColor lightGrayColor];
    dayCountLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:dayCountLabel];
    dayCountLabel.text = @"0天";
    _dayCountLabel = dayCountLabel;
}

- (void)addGetIntegralButton{
    UIButton *getIntegralButton = [UIButton buttonWithType:UIButtonTypeCustom];
    getIntegralButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [getIntegralButton addTarget:self action:@selector(getIntegralButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:getIntegralButton];
    
    _getIntegralButton = getIntegralButton;
}

- (void)addActivityIndicatorView{
    UIActivityIndicatorView *activityIndicatorView = [UIActivityIndicatorView new];
    [self addSubview:activityIndicatorView];
    _activityIndicatorView = activityIndicatorView;
}
@end
